/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.ui;

import java.util.LinkedList;
import java.util.List;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 *
 * @author bennyl
 */
public class TopComponents {

    public static <T extends TopComponent> T lookup(Class<T> cls) {
        return (T) WindowManager.getDefault().findTopComponent(cls.getAnnotation(TopComponent.Description.class).preferredID());
    }

    public static List<TopComponent> lookupAll(Class... cls) {
        LinkedList<TopComponent> ll = new LinkedList();
        for (Class c : cls) {
            ll.add(lookup(c));
        }

        return ll;
    }

    public static <T extends TopComponent> T activate(Class<T> cls) {
        T top = lookup(cls);
        top.open();
        top.requestActive();
        return top;
    }

    public static <T extends TopComponent> T show(Class<T> cls) {
        final T top = lookup(cls);
        if (!top.isOpened()) {
            top.open();
        }

        if (!top.isShowing() || !top.isVisible()) {
            top.requestVisible();
            top.setVisible(true);
        }

        return top;
    }

    public static void hide(Class<? extends TopComponent> cls) {
        final TopComponent top = lookup(cls);

        if (top.isOpened() && top.isVisible()) {
            top.setVisible(false);
        }
    }

    public static void close(Class<? extends TopComponent> cls) {
        final TopComponent top = lookup(cls);
        if (top.isOpened()) {
            top.close();
        }
    }
}
